# -*- coding: utf-8 -*-
# This script was auto-generated by Sim4Life version 6.0.0.3169
import s4l_v1.document as document
import s4l_v1.materials.database as database
import s4l_v1.model as model
import s4l_v1.simulation.emlf as emlf
import s4l_v1.units as units
from s4l_v1 import ReleaseVersion
from s4l_v1 import Unit
import s4l_v1 as s4l
import s4l_v1.analysis as analysis
import XPostProcessor as xp
import numpy as np
import numpy

s4l.ignore_deprecation_warnings()

##########################################################################################
# Author:           Dr. Antonino M. Cassara
# Affiliation:      IT'IS Foundation, Zurich, Switzerland
# email:            cassara@itis.swiss
# Date:             20201118
# Sim4Life Version: 6.0.0.3169
#
# Reference: 
#            Schreglmann SR et al. (2020). Non-invasive Suppression of Essential Tremor 
#            via Phase-Locked Disruption of its Temporal Coherence. Nature Communication
#            (in press).
#
# Note: The simulation has been modified to be run with Sim4LifeLight, by reducing the
#       grid resolution to have max 20MCells. For quantitative predictions, please
#       consider to improve the resolution to 0.5mm (or smaller). 
##########################################################################################

def CreatesEMSimulation():
	# Define the version to use for default values
	#ReleaseVersion.set_active(ReleaseVersion.version6_0)
	
	# Creating the simulation
	simulation = emlf.ElectroQsOhmicSimulation()
	simulation.Name = "EM_Simulation_Reduced"

	# Mapping the components and entities
	component__plane_x = simulation.AllComponents["Plane X+"]
	component__plane_x_1 = simulation.AllComponents["Plane X-"]
	component__background = simulation.AllComponents["Background"]
	component__plane_y = simulation.AllComponents["Plane Y+"]
	component__plane_y_1 = simulation.AllComponents["Plane Y-"]
	component__plane_z = simulation.AllComponents["Plane Z+"]
	component__plane_z_1 = simulation.AllComponents["Plane Z-"]
	component__overall_field = simulation.AllComponents["Overall Field"]
	entity__adipose_tissue = model.AllEntities()["Adipose Tissue"]
	entity__brain_white_matter = model.AllEntities()["Brain White Matter"]
	entity__vertebra_c4 = model.AllEntities()["Vertebra - C4"]
	entity__muscle_depressor_labii = model.AllEntities()["Muscle - Depressor Labii"]
	entity__cranial_nerve_vi_abducens = model.AllEntities()["Cranial Nerve VI - Abducens"]
	entity__ear_cochlea = model.AllEntities()["Ear Cochlea"]
	entity__vertebra_c1atlas = model.AllEntities()["Vertebra - C1 (atlas)"]
	entity__cranial_nerve_viii_vestibulocochlear = model.AllEntities()["Cranial Nerve VIII - Vestibulocochlear"]
	entity__commissura_posterior = model.AllEntities()["Commissura (Posterior)"]
	entity__nasal_septum_cartilage = model.AllEntities()["Nasal Septum (Cartilage)"]
	entity__cerebral_peduncles = model.AllEntities()["Cerebral Peduncles"]
	entity__ear_auricular_cartilage_pinna = model.AllEntities()["Ear Auricular Cartilage (Pinna)"]
	entity__air_internal_mastoid = model.AllEntities()["Air Internal - Mastoid"]
	entity__cerebellum_white_matter = model.AllEntities()["Cerebellum White Matter"]
	entity__cranial_nerve_ix_glossopharyngeal = model.AllEntities()["Cranial Nerve IX - Glossopharyngeal"]
	entity__caudate_nucleus = model.AllEntities()["Caudate Nucleus"]
	entity__ear_semicircular_canals = model.AllEntities()["Ear Semicircular Canals"]
	entity__muscle_lateral_rectus = model.AllEntities()["Muscle - Lateral Rectus"]
	entity__epidermis__dermis = model.AllEntities()["Epidermis_Dermis"]
	entity__muscle_inferior_oblique = model.AllEntities()["Muscle - Inferior Oblique"]
	entity__cranial_nerve_xii_hypoglossal = model.AllEntities()["Cranial Nerve XII - Hypoglossal "]
	entity__hypophysisor_pituitary_gland = model.AllEntities()["Hypophysis or Pituitary Gland "]
	entity__muscle_zygomaticus_minor = model.AllEntities()["Muscle - Zygomaticus Minor"]
	entity__cranial_nerve_ii_optic = model.AllEntities()["Cranial Nerve II - Optic"]
	entity__blood_arteries = model.AllEntities()["Blood Arteries"]
	entity__vertebra_c3 = model.AllEntities()["Vertebra - C3"]
	entity__vertebra_c2axis = model.AllEntities()["Vertebra - C2 (axis)"]
	entity__electrode1 = model.AllEntities()["Electrode1"]
	entity__blood_veins = model.AllEntities()["Blood Veins"]
	entity__muscle_medial_rectus = model.AllEntities()["Muscle - Medial Rectus"]
	entity__muscle_superior_rectus = model.AllEntities()["Muscle - Superior Rectus"]
	entity__cranial_nerve_v3_mandibular_division = model.AllEntities()["Cranial Nerve V3 - Mandibular Division"]
	entity__muscle_zygomaticus_major = model.AllEntities()["Muscle - Zygomaticus Major "]
	entity__muscle_superior_oblique = model.AllEntities()["Muscle - Superior Oblique"]
	entity__putamen = model.AllEntities()["Putamen"]
	entity__brainstem_pons = model.AllEntities()["Brainstem Pons"]
	entity__spinal_cord = model.AllEntities()["Spinal Cord"]
	entity__commissura_anterior = model.AllEntities()["Commissura (Anterior)"]
	entity__muscle_levator_scapulae = model.AllEntities()["Muscle - Levator Scapulae"]
	entity__submandibular_gland = model.AllEntities()["Submandibular Gland"]
	entity__thalamus = model.AllEntities()["Thalamus"]
	entity__muscles_procerus = model.AllEntities()["Muscles - Procerus"]
	entity__mucosa = model.AllEntities()["Mucosa"]
	entity__electrode2 = model.AllEntities()["Electrode2"]
	entity__skull_inner_table = model.AllEntities()["Skull Inner Table"]
	entity__cranial_nerve_xi_accessory = model.AllEntities()["Cranial Nerve XI - Accessory "]
	entity__mandible = model.AllEntities()["Mandible"]
	entity_csf_general = model.AllEntities()["CSF General"]
	entity__cranial_nerve_v_trigeminal = model.AllEntities()["Cranial Nerve V - Trigeminal"]
	entity__cranial_nerve_x_vagus = model.AllEntities()["Cranial Nerve X - Vagus"]
	entity__brainstem_midbrain = model.AllEntities()["Brainstem Midbrain"]
	entity__right__amigdala = model.AllEntities()["Right_Amigdala"]
	entity__pineal_body = model.AllEntities()["Pineal Body"]
	entity__hyoid_bone = model.AllEntities()["Hyoid Bone"]
	entity__cranial_nerve_v2_maxillary_division = model.AllEntities()["Cranial Nerve V2 - Maxillary Division"]
	entity__muscle_sternocleidomastoid = model.AllEntities()["Muscle - Sternocleidomastoid "]
	entity__eye_cornea = model.AllEntities()["Eye Cornea"]
	entity__optic_chiasm = model.AllEntities()["Optic Chiasm"]
	entity__eye_lens = model.AllEntities()["Eye Lens"]
	entity__eye_vitreous = model.AllEntities()["Eye Vitreous "]
	entity__muscle_platysma = model.AllEntities()["Muscle - Platysma"]
	entity__left__hippocampus = model.AllEntities()["Left_Hippocampus"]
	entity__tendon_galea_aponeurotica = model.AllEntities()["Tendon - Galea Aponeurotica"]
	entity__muscle_depressor_anguli_oris = model.AllEntities()["Muscle - Depressor Anguli Oris"]
	entity__muscle_trapezius = model.AllEntities()["Muscle - Trapezius"]
	entity_csf_ventricles = model.AllEntities()["CSF Ventricles"]
	entity__muscle_buccinator = model.AllEntities()["Muscle - Buccinator"]
	entity__skull_diploe = model.AllEntities()["Skull Diploe"]
	entity__subcutaneous_adipose_tissue = model.AllEntities()["Subcutaneous Adipose Tissue"]
	entity__cranial_nerve_vii_facial = model.AllEntities()["Cranial Nerve VII - Facial"]
	entity__hypothalamus = model.AllEntities()["Hypothalamus"]
	entity__cerebellum_gray_matter = model.AllEntities()["Cerebellum Gray Matter"]
	entity__muscle_mentalis = model.AllEntities()["Muscle - Mentalis"]
	entity__air_internal_sphenoidal_sinus = model.AllEntities()["Air Internal - Sphenoidal Sinus"]
	entity__eye_retina__choroid__sclera = model.AllEntities()["Eye Retina_Choroid_Sclera"]
	entity__ear_pharyngotympanic_tube = model.AllEntities()["Ear Pharyngotympanic Tube"]
	entity__intervertebral_discs = model.AllEntities()["Intervertebral Discs"]
	entity__cranial_nerve_iv_trochlear = model.AllEntities()["Cranial Nerve IV - Trochlear"]
	entity__skull = model.AllEntities()["Skull"]
	entity__teeth = model.AllEntities()["Teeth"]
	entity__muscle_medial_pterygoid = model.AllEntities()["Muscle - Medial Pterygoid"]
	entity__muscle_inferior_rectus = model.AllEntities()["Muscle - Inferior Rectus"]
	entity__air_internal_oral_cavity = model.AllEntities()["Air Internal - Oral Cavity"]
	entity__skull_outer_table = model.AllEntities()["Skull Outer Table "]
	entity__muscles_risorius = model.AllEntities()["Muscles - Risorius"]
	entity__nucleus_accumbens = model.AllEntities()["Nucleus Accumbens"]
	entity__mammillary_body = model.AllEntities()["Mammillary Body"]
	entity__muscle_temporalis__temporoparietalis = model.AllEntities()["Muscle - Temporalis_Temporoparietalis"]
	entity__substantia_nigra = model.AllEntities()["Substantia Nigra"]
	entity__right__hippocampus = model.AllEntities()["Right_Hippocampus"]
	entity__muscle_lateral_pterygoid = model.AllEntities()["Muscle - Lateral Pterygoid"]
	entity__muscle_orbicularis_oris = model.AllEntities()["Muscle - Orbicularis Oris"]
	entity__parotid_gland = model.AllEntities()["Parotid Gland"]
	entity__ear_auditory_canal = model.AllEntities()["Ear Auditory Canal"]
	entity__brain_gray_matter = model.AllEntities()["Brain Gray Matter"]
	entity__globus_pallidus = model.AllEntities()["Globus Pallidus"]
	entity__tongue = model.AllEntities()["Tongue"]
	entity__muscle_nasalis = model.AllEntities()["Muscle - Nasalis"]
	entity__air_internal_ethmoidal_sinus = model.AllEntities()["Air Internal - Ethmoidal Sinus"]
	entity__muscle_occipitiofrontalis_occipital_belly = model.AllEntities()["Muscle - Occipitiofrontalis - Occipital Belly"]
	entity__cranial_nerve_i_olfactory = model.AllEntities()["Cranial Nerve I - Olfactory"]
	entity__eye_aqueous = model.AllEntities()["Eye Aqueous"]
	entity__muscle_splenius_capitis = model.AllEntities()["Muscle - Splenius Capitis"]
	entity__dura = model.AllEntities()["Dura"]
	entity__cranial_nerve_iii_oculomotor = model.AllEntities()["Cranial Nerve III - Oculomotor"]
	entity__muscle_levator_labii_superioris = model.AllEntities()["Muscle - Levator Labii Superioris"]
	entity__air_internal_maxillary_sinus = model.AllEntities()["Air Internal - Maxillary Sinus"]
	entity__tendon_temporalis_tendon = model.AllEntities()["Tendon - Temporalis Tendon"]
	entity__air_internal_nasal__pharynx = model.AllEntities()["Air Internal - Nasal_Pharynx"]
	entity__vertebra_c5 = model.AllEntities()["Vertebra - C5"]
	entity__sublingual_gland = model.AllEntities()["Sublingual Gland"]
	entity__muscle_masseter = model.AllEntities()["Muscle - Masseter"]
	entity__air_internal_frontal_sinus = model.AllEntities()["Air Internal - Frontal Sinus"]
	entity__left__amigdala = model.AllEntities()["Left_Amigdala"]
	entity__optic_tract = model.AllEntities()["Optic Tract"]
	entity__muscle_occipitiofrontalis_frontal_belly = model.AllEntities()["Muscle - Occipitiofrontalis - Frontal Belly"]
	entity__muscle_general = model.AllEntities()["Muscle (General)"]
	entity__muscle_orbicularis_oculi = model.AllEntities()["Muscle - Orbicularis Oculi"]
	entity__brainstem_medulla = model.AllEntities()["Brainstem Medulla"]

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__eye_aqueous, entity__eye_vitreous]
	mat = database["IT'IS LF 4.0"]["Eye (Vitreous Humor)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Eye (Vitreous Humor)"
		material_settings.MassDensity = 1004.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 1.55, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 98.99052268497141
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__skull_diploe]
	mat = database["IT'IS LF 4.0"]["Bone (Cancellous)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Bone (Cancellous)"
		material_settings.MassDensity = 1178.33333333333, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0820712643678161, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 12320.035797440474
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__tendon_galea_aponeurotica, entity__tendon_temporalis_tendon]
	mat = database["IT'IS LF 4.0"]["Tendon\Ligament"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Tendon\Ligament"
		material_settings.MassDensity = 1142.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.367577227722772, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 302705.1901828624
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__eye_retina__choroid__sclera]
	mat = database["IT'IS LF 4.0"]["Eye (Sclera)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Eye (Sclera)"
		material_settings.MassDensity = 1032.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.62, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 54552.706488304204
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__ear_auricular_cartilage_pinna, entity__nasal_septum_cartilage]
	mat = database["IT'IS LF 4.0"]["Cartilage"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Cartilage"
		material_settings.MassDensity = 1099.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 1.01, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 32041.841389942976
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__intervertebral_discs]
	mat = database["IT'IS LF 4.0"]["Intervertebral Disc"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Intervertebral Disc"
		material_settings.MassDensity = 1099.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 1.01, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 60.84441798243736
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__teeth]
	mat = database["IT'IS LF 4.0"]["Tooth"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Tooth"
		material_settings.MassDensity = 2180.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0035039941902687, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 2702.3711256306647
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__brain_gray_matter, entity__caudate_nucleus, entity__cerebellum_gray_matter, entity__globus_pallidus, entity__mammillary_body, entity__nucleus_accumbens, entity__putamen, entity__substantia_nigra]
	mat = database["IT'IS LF 4.0"]["Brain (Grey Matter)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Brain (Grey Matter)"
		material_settings.MassDensity = 1044.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.23914873606449, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164062.99316639948
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__tongue]
	mat = database["IT'IS LF 4.0"]["Tongue"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Tongue"
		material_settings.MassDensity = 1090.4, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.355286874117476, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 58052.57389848724
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__brain_white_matter, entity__cerebellum_white_matter, entity__cerebral_peduncles]
	mat = database["IT'IS LF 4.0"]["Brain (White Matter)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Brain (White Matter)"
		material_settings.MassDensity = 1041.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.265075903212916, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 69810.68883114035
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__commissura_posterior]
	mat = database["IT'IS LF 4.0"]["Commissura Posterior"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Commissura Posterior"
		material_settings.MassDensity = 1041.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.265075903212916, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 69810.68883114035
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__hypophysisor_pituitary_gland]
	mat = database["IT'IS LF 4.0"]["Hypophysis"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Hypophysis"
		material_settings.MassDensity = 1053.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.4811, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 56558.047764028575
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__thalamus]
	mat = database["IT'IS LF 4.0"]["Thalamus"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Thalamus"
		material_settings.MassDensity = 1044.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.23914873606449, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164062.99316639948
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__brainstem_pons]
	mat = database["IT'IS LF 4.0"]["Pons"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Pons"
		material_settings.MassDensity = 1045.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.234006562252398, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164357.96524411367
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__commissura_anterior]
	mat = database["IT'IS LF 4.0"]["Commissura Anterior"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Commissura Anterior"
		material_settings.MassDensity = 1041.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.265075903212916, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 69810.68883114035
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__left__amigdala, entity__left__hippocampus, entity__right__amigdala, entity__right__hippocampus]
	mat = database["IT'IS LF 4.0"]["Brain"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Brain"
		material_settings.MassDensity = 1045.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.234006562252398, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164357.96524411367
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__pineal_body]
	mat = database["IT'IS LF 4.0"]["Pineal Body"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Pineal Body"
		material_settings.MassDensity = 1053.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.4811, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 56558.047764028575
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__hypothalamus]
	mat = database["IT'IS LF 4.0"]["Hypothalamus"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Hypothalamus"
		material_settings.MassDensity = 1044.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.23914873606449, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164062.99316639948
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__dura]
	mat = database["IT'IS LF 4.0"]["Dura"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Dura"
		material_settings.MassDensity = 1174.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.367577227722772, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 5343.990898234167
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__brainstem_medulla]
	mat = database["IT'IS LF 4.0"]["Medulla Oblongata"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Medulla Oblongata"
		material_settings.MassDensity = 1045.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.234006562252398, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164357.96524411367
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__muscle_buccinator, entity__muscle_depressor_anguli_oris, entity__muscle_depressor_labii, entity__muscle_general, entity__muscle_inferior_oblique, entity__muscle_inferior_rectus, entity__muscle_lateral_pterygoid, entity__muscle_lateral_rectus, entity__muscle_levator_labii_superioris, entity__muscle_levator_scapulae, entity__muscle_masseter, entity__muscle_medial_pterygoid, entity__muscle_medial_rectus, entity__muscle_mentalis, entity__muscle_nasalis, entity__muscle_occipitiofrontalis_frontal_belly, entity__muscle_occipitiofrontalis_occipital_belly, entity__muscle_orbicularis_oculi, entity__muscle_orbicularis_oris, entity__muscle_platysma, entity__muscle_splenius_capitis, entity__muscle_sternocleidomastoid, entity__muscle_superior_oblique, entity__muscle_superior_rectus, entity__muscle_temporalis__temporoparietalis, entity__muscle_trapezius, entity__muscle_zygomaticus_major, entity__muscle_zygomaticus_minor, entity__muscles_procerus, entity__muscles_risorius]
	mat = database["IT'IS LF 4.0"]["Muscle"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Muscle"
		material_settings.MassDensity = 1090.4, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.355286874117476, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 434932.192427419
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__ear_cochlea, entity__ear_semicircular_canals, entity_csf_general, entity_csf_ventricles]
	mat = database["IT'IS LF 4.0"]["Cerebrospinal Fluid"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Cerebrospinal Fluid"
		material_settings.MassDensity = 1007.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 1.7765, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 108.9999972649334
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__adipose_tissue, entity__subcutaneous_adipose_tissue]
	mat = database["IT'IS LF 4.0"]["SAT (Subcutaneous Fat)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "SAT (Subcutaneous Fat)"
		material_settings.MassDensity = 911.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0573412363008279, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 19286.645785175333
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__vertebra_c1atlas, entity__vertebra_c2axis, entity__vertebra_c3, entity__vertebra_c4, entity__vertebra_c5]
	mat = database["IT'IS LF 4.0"]["Vertebrae"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Vertebrae"
		material_settings.MassDensity = 1908.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0035039941902687, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 2702.3711256306647
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__cranial_nerve_i_olfactory, entity__cranial_nerve_ii_optic, entity__cranial_nerve_iii_oculomotor, entity__cranial_nerve_iv_trochlear, entity__cranial_nerve_ix_glossopharyngeal, entity__cranial_nerve_v2_maxillary_division, entity__cranial_nerve_v3_mandibular_division, entity__cranial_nerve_v_trigeminal, entity__cranial_nerve_vi_abducens, entity__cranial_nerve_vii_facial, entity__cranial_nerve_viii_vestibulocochlear, entity__cranial_nerve_x_vagus, entity__cranial_nerve_xi_accessory, entity__cranial_nerve_xii_hypoglossal, entity__optic_chiasm, entity__optic_tract]
	mat = database["IT'IS LF 4.0"]["Nerve"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Nerve"
		material_settings.MassDensity = 1075.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.265075903212916, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 69911.4914652573
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__air_internal_ethmoidal_sinus, entity__air_internal_frontal_sinus, entity__air_internal_mastoid, entity__air_internal_maxillary_sinus, entity__air_internal_nasal__pharynx, entity__air_internal_oral_cavity, entity__air_internal_sphenoidal_sinus, entity__ear_auditory_canal, entity__ear_pharyngotympanic_tube]
	mat = database["IT'IS LF 4.0"]["Air"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Air"
		material_settings.MassDensity = 1.16409155293818, Unit("kg/m^3")
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__brainstem_midbrain]
	mat = database["IT'IS LF 4.0"]["Midbrain"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Midbrain"
		material_settings.MassDensity = 1045.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.234006562252398, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 164357.96524411367
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__epidermis__dermis]
	mat = database["IT'IS LF 4.0"]["Skin"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Skin"
		material_settings.MassDensity = 1109.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.17, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 1135.619382618975
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__blood_arteries, entity__blood_veins]
	mat = database["IT'IS LF 4.0"]["Blood"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Blood"
		material_settings.MassDensity = 1049.75, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.659851590289857, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 5258.608390020375
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__parotid_gland, entity__sublingual_gland, entity__submandibular_gland]
	mat = database["IT'IS LF 4.0"]["Salivary Gland"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Salivary Gland"
		material_settings.MassDensity = 1048.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.67, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 92.05825348518617
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__spinal_cord]
	mat = database["IT'IS LF 4.0"]["Spinal Cord"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Spinal Cord"
		material_settings.MassDensity = 1075.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.234006562252398, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 69911.4914652573
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__mandible]
	mat = database["IT'IS LF 4.0"]["Mandible"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Mandible"
		material_settings.MassDensity = 1908.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0035039941902687, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 2702.3711256306647
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__hyoid_bone, entity__skull, entity__skull_inner_table, entity__skull_outer_table]
	mat = database["IT'IS LF 4.0"]["Bone (Cortical)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Bone (Cortical)"
		material_settings.MassDensity = 1908.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.0035039941902687, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 2702.3711256306647
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__mucosa]
	mat = database["IT'IS LF 4.0"]["Mucous Membrane"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Mucous Membrane"
		material_settings.MassDensity = 1102.0, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.355286874117476, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 434932.192427419
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__eye_cornea]
	mat = database["IT'IS LF 4.0"]["Eye (Cornea)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Eye (Cornea)"
		material_settings.MassDensity = 1050.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.62, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 101454.07990360959
	simulation.Add(material_settings, components)

	# Adding a new MaterialSettings
	material_settings = emlf.MaterialSettings()
	components = [entity__eye_lens]
	mat = database["IT'IS LF 4.0"]["Eye (Lens)"]
	if mat is not None:
		simulation.LinkMaterialWithDatabase(material_settings, mat)
	else:
		# Fallback if material is not found
		material_settings.Name = "Eye (Lens)"
		material_settings.MassDensity = 1075.5, Unit("kg/m^3")
		material_settings.ElectricProps.Conductivity = 0.345333333333333, Unit("S/m")
		material_settings.ElectricProps.RelativePermittivity = 1069.512692895308
	simulation.Add(material_settings, components)

	# Editing BoundarySettings "Boundary Settings"
	boundary_settings = [x for x in simulation.AllSettings if isinstance(x, emlf.BoundarySettings) and x.Name == "Boundary Settings"][0]
	components = [component__plane_x, component__plane_x_1, component__plane_y, component__plane_y_1, component__plane_z, component__plane_z_1]
	simulation.Add(boundary_settings, components)
	boundary_settings.BoundaryType = boundary_settings.BoundaryType.enum.Flux
	boundary_settings.FluxValue = 0.0

	# Adding a new BoundarySettings
	boundary_settings = emlf.BoundarySettings()
	components = [entity__electrode1]
	boundary_settings.Name = "Anode"
	boundary_settings.DirichletValue = -0.5, units.Volts
	simulation.Add(boundary_settings, components)

	# Adding a new BoundarySettings
	boundary_settings = emlf.BoundarySettings()
	components = [entity__electrode2]
	boundary_settings.Name = "Cathode"
	simulation.Add(boundary_settings, components)

	# Editing GlobalGridSettings "Grid (Empty)"
	global_grid_settings = simulation.GlobalGridSettings
	global_grid_settings.DiscretizationMode = global_grid_settings.DiscretizationMode.enum.Manual
	global_grid_settings.MaxStep = numpy.array([15.0, 15.0, 15.0]), units.MilliMeters
	global_grid_settings.Resolution = numpy.array([1.0, 1.0, 1.0]), units.MilliMeters
	global_grid_settings.PaddingMode = global_grid_settings.PaddingMode.enum.Manual
	global_grid_settings.BottomPadding = numpy.array([0.0, 0.0, 70.0]), units.MilliMeters
	global_grid_settings.TopPadding = numpy.array([0.0, 0.0, 0.0]), units.MilliMeters

	# Adding a new ManualGridSettings
	manual_grid_settings = simulation.AddManualGridSettings([entity__adipose_tissue, entity__air_internal_ethmoidal_sinus, entity__air_internal_frontal_sinus, entity__air_internal_mastoid, entity__air_internal_maxillary_sinus, entity__air_internal_nasal__pharynx, entity__air_internal_oral_cavity, entity__air_internal_sphenoidal_sinus, entity__blood_arteries, entity__blood_veins, entity__brain_gray_matter, entity__brain_white_matter, entity__brainstem_medulla, entity__brainstem_midbrain, entity__brainstem_pons, entity__caudate_nucleus, entity__cerebellum_gray_matter, entity__cerebellum_white_matter, entity__cerebral_peduncles, entity__commissura_anterior, entity__commissura_posterior, entity__cranial_nerve_i_olfactory, entity__cranial_nerve_ii_optic, entity__cranial_nerve_iii_oculomotor, entity__cranial_nerve_iv_trochlear, entity__cranial_nerve_ix_glossopharyngeal, entity__cranial_nerve_v2_maxillary_division, entity__cranial_nerve_v3_mandibular_division, entity__cranial_nerve_v_trigeminal, entity__cranial_nerve_vi_abducens, entity__cranial_nerve_vii_facial, entity__cranial_nerve_viii_vestibulocochlear, entity__cranial_nerve_x_vagus, entity__cranial_nerve_xi_accessory, entity__cranial_nerve_xii_hypoglossal, entity__dura, entity__ear_auditory_canal, entity__ear_auricular_cartilage_pinna, entity__ear_cochlea, entity__ear_pharyngotympanic_tube, entity__ear_semicircular_canals, entity__epidermis__dermis, entity__eye_aqueous, entity__eye_cornea, entity__eye_lens, entity__eye_retina__choroid__sclera, entity__eye_vitreous, entity__globus_pallidus, entity__hyoid_bone, entity__hypophysisor_pituitary_gland, entity__hypothalamus, entity__intervertebral_discs, entity__left__amigdala, entity__left__hippocampus, entity__mammillary_body, entity__mandible, entity__mucosa, entity__muscle_buccinator, entity__muscle_depressor_anguli_oris, entity__muscle_depressor_labii, entity__muscle_general, entity__muscle_inferior_oblique, entity__muscle_inferior_rectus, entity__muscle_lateral_pterygoid, entity__muscle_lateral_rectus, entity__muscle_levator_labii_superioris, entity__muscle_levator_scapulae, entity__muscle_masseter, entity__muscle_medial_pterygoid, entity__muscle_medial_rectus, entity__muscle_mentalis, entity__muscle_nasalis, entity__muscle_occipitiofrontalis_frontal_belly, entity__muscle_occipitiofrontalis_occipital_belly, entity__muscle_orbicularis_oculi, entity__muscle_orbicularis_oris, entity__muscle_platysma, entity__muscle_splenius_capitis, entity__muscle_sternocleidomastoid, entity__muscle_superior_oblique, entity__muscle_superior_rectus, entity__muscle_temporalis__temporoparietalis, entity__muscle_trapezius, entity__muscle_zygomaticus_major, entity__muscle_zygomaticus_minor, entity__muscles_procerus, entity__muscles_risorius, entity__nasal_septum_cartilage, entity__nucleus_accumbens, entity__optic_chiasm, entity__optic_tract, entity__parotid_gland, entity__pineal_body, entity__putamen, entity__right__amigdala, entity__right__hippocampus, entity__skull, entity__skull_diploe, entity__skull_inner_table, entity__skull_outer_table, entity__spinal_cord, entity__subcutaneous_adipose_tissue, entity__sublingual_gland, entity__submandibular_gland, entity__substantia_nigra, entity__teeth, entity__tendon_galea_aponeurotica, entity__tendon_temporalis_tendon, entity__thalamus, entity__tongue, entity__vertebra_c1atlas, entity__vertebra_c2axis, entity__vertebra_c3, entity__vertebra_c4, entity__vertebra_c5, entity_csf_general, entity_csf_ventricles])
	manual_grid_settings.Name = "MIDA"
	manual_grid_settings.MaxStep = numpy.array([0.8, 0.8, 0.8]), units.MilliMeters
	manual_grid_settings.Resolution = numpy.array([0.1, 0.1, 0.1]), units.MilliMeters
	manual_grid_settings.Priority = 0.0

	# Adding a new ManualGridSettings
	manual_grid_settings = simulation.AddManualGridSettings([entity__electrode1, entity__electrode2])
	manual_grid_settings.Name = "Electrodes"
	manual_grid_settings.MaxStep = numpy.array([0.75, 0.75, 0.75]), units.MilliMeters
	manual_grid_settings.Resolution = numpy.array([0.1, 0.1, 0.1]), units.MilliMeters
	manual_grid_settings.Priority = 0.0

	# Editing AutomaticVoxelerSettings "Automatic Voxeler Settings"
	automatic_voxeler_settings = [x for x in simulation.AllSettings if isinstance(x, emlf.AutomaticVoxelerSettings) and x.Name == "Automatic Voxeler Settings"][0]
	automatic_voxeler_settings.Name = "MIDA"
	components = [entity__adipose_tissue, entity__air_internal_ethmoidal_sinus, entity__air_internal_frontal_sinus, entity__air_internal_mastoid, entity__air_internal_maxillary_sinus, entity__air_internal_nasal__pharynx, entity__air_internal_oral_cavity, entity__air_internal_sphenoidal_sinus, entity__blood_arteries, entity__blood_veins, entity__brain_gray_matter, entity__brain_white_matter, entity__brainstem_medulla, entity__brainstem_midbrain, entity__brainstem_pons, entity__caudate_nucleus, entity__cerebellum_gray_matter, entity__cerebellum_white_matter, entity__cerebral_peduncles, entity__commissura_anterior, entity__commissura_posterior, entity__cranial_nerve_i_olfactory, entity__cranial_nerve_ii_optic, entity__cranial_nerve_iii_oculomotor, entity__cranial_nerve_iv_trochlear, entity__cranial_nerve_ix_glossopharyngeal, entity__cranial_nerve_v2_maxillary_division, entity__cranial_nerve_v3_mandibular_division, entity__cranial_nerve_v_trigeminal, entity__cranial_nerve_vi_abducens, entity__cranial_nerve_vii_facial, entity__cranial_nerve_viii_vestibulocochlear, entity__cranial_nerve_x_vagus, entity__cranial_nerve_xi_accessory, entity__cranial_nerve_xii_hypoglossal, entity__dura, entity__ear_auditory_canal, entity__ear_auricular_cartilage_pinna, entity__ear_cochlea, entity__ear_pharyngotympanic_tube, entity__ear_semicircular_canals, entity__epidermis__dermis, entity__eye_aqueous, entity__eye_cornea, entity__eye_lens, entity__eye_retina__choroid__sclera, entity__eye_vitreous, entity__globus_pallidus, entity__hyoid_bone, entity__hypophysisor_pituitary_gland, entity__hypothalamus, entity__intervertebral_discs, entity__left__amigdala, entity__left__hippocampus, entity__mammillary_body, entity__mandible, entity__mucosa, entity__muscle_buccinator, entity__muscle_depressor_anguli_oris, entity__muscle_depressor_labii, entity__muscle_general, entity__muscle_inferior_oblique, entity__muscle_inferior_rectus, entity__muscle_lateral_pterygoid, entity__muscle_lateral_rectus, entity__muscle_levator_labii_superioris, entity__muscle_levator_scapulae, entity__muscle_masseter, entity__muscle_medial_pterygoid, entity__muscle_medial_rectus, entity__muscle_mentalis, entity__muscle_nasalis, entity__muscle_occipitiofrontalis_frontal_belly, entity__muscle_occipitiofrontalis_occipital_belly, entity__muscle_orbicularis_oculi, entity__muscle_orbicularis_oris, entity__muscle_platysma, entity__muscle_splenius_capitis, entity__muscle_sternocleidomastoid, entity__muscle_superior_oblique, entity__muscle_superior_rectus, entity__muscle_temporalis__temporoparietalis, entity__muscle_trapezius, entity__muscle_zygomaticus_major, entity__muscle_zygomaticus_minor, entity__muscles_procerus, entity__muscles_risorius, entity__nasal_septum_cartilage, entity__nucleus_accumbens, entity__optic_chiasm, entity__optic_tract, entity__parotid_gland, entity__pineal_body, entity__putamen, entity__right__amigdala, entity__right__hippocampus, entity__skull, entity__skull_diploe, entity__skull_inner_table, entity__skull_outer_table, entity__spinal_cord, entity__subcutaneous_adipose_tissue, entity__sublingual_gland, entity__submandibular_gland, entity__substantia_nigra, entity__teeth, entity__tendon_galea_aponeurotica, entity__tendon_temporalis_tendon, entity__thalamus, entity__tongue, entity__vertebra_c1atlas, entity__vertebra_c2axis, entity__vertebra_c3, entity__vertebra_c4, entity__vertebra_c5, entity_csf_general, entity_csf_ventricles]
	simulation.Add(automatic_voxeler_settings, components)
	automatic_voxeler_settings.Priority = 20

	# Adding a new ManualVoxelerSettings
	manual_voxeler_settings = emlf.ManualVoxelerSettings()
	components = [entity__electrode1, entity__electrode2]
	manual_voxeler_settings.Name = "Electrodes"
	simulation.Add(manual_voxeler_settings, components)

	# Editing SolverSettings "Solver"
	solver_settings = simulation.SolverSettings
	solver_settings.PredefinedTolerances = solver_settings.PredefinedTolerances.enum.High

	# Update the materials with the new frequency parameters
	simulation.UpdateAllMaterials()

	# Update the grid with the new parameters
	simulation.UpdateGrid()

	# Add the simulation to the UI
	document.AllSimulations.Add( simulation )
	
	return simulation

def Analyze(simulation,sensor,entslist):

	ents=s4l.model.AllEntities()
	
	simulation_extractor = simulation.Results()
	em_sensor_extractor = simulation_extractor["Overall Field"]
	em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All"

	#### Slice View #########################################################################################

	# Adding a new ModelToGridFilter
	model_to_grid_filter = analysis.core.ModelToGridFilter()
	model_to_grid_filter.Name = sensor.Name
	model_to_grid_filter.Entity = sensor
	model_to_grid_filter.MaximumEdgeLength = 2.5e-04, units.Meters
	model_to_grid_filter.UpdateAttributes()
	model_to_grid_filter.Update()
	document.AllAlgorithms.Add(model_to_grid_filter)

	# Adding a new FieldInterpolationFilter
	inputs = [em_sensor_extractor.Outputs["J(x,y,z,f0)"], model_to_grid_filter.Outputs["Surface"]]
	field_interpolation_filter = analysis.core.FieldInterpolationFilter(inputs=inputs)
	field_interpolation_filter.UpdateAttributes()
	field_interpolation_filter.Update()

	# Adding a new SurfaceFieldFluxEvaluator
	inputs = [field_interpolation_filter.Outputs["J(x,y,z,f0)"]]
	surface_field_flux_evaluator = analysis.core.SurfaceFieldFluxEvaluator(inputs=inputs)
	surface_field_flux_evaluator.UpdateAttributes()
	surface_field_flux_evaluator.Update()
	flux=surface_field_flux_evaluator.Outputs[1]
	flux=1000*np.abs(np.real(flux.Data.GetComponent(0)[0])) # in mA
	document.AllAlgorithms.Add(surface_field_flux_evaluator)
	
	# Field Masking
	mf = s4l.analysis.core.FieldMaskingFilter()
	mf.SetInputConnection(em_sensor_extractor.Outputs["EM E(x,y,z,f0)"],)
	mf.SetAllMaterials(False)
	mf.SetEntities(entslist)
	mf.UpdateAttributes()
	mf.Update(0)
	newfield=mf.GetOutputPort(0)
	document.AllAlgorithms.Add(mf)

	# Adding a new UserDefinedFieldNormalizer
	inputs = [mf.Outputs["EM E(x,y,z,f0)"]]
	user_defined_field_normalizer = analysis.field.UserDefinedFieldNormalizer(inputs=inputs)
	user_defined_field_normalizer.Target.Value = 2./flux
	user_defined_field_normalizer.UpdateAttributes()
	user_defined_field_normalizer.Update()
	document.AllAlgorithms.Add(user_defined_field_normalizer)
	
	# Creates a slice view
	inputs = [user_defined_field_normalizer.Outputs["EM E(x,y,z,f0)"]]
	slice_field_viewer = analysis.viewers.SliceFieldViewer(inputs=inputs)
	slice_field_viewer.Data.Mode = slice_field_viewer.Data.Mode.enum.QuantityComplexModulus
	slice_field_viewer.Data.Component = slice_field_viewer.Data.Component.enum.ComponentsAll
	slice_field_viewer.Slice.Type = slice_field_viewer.Slice.Type.enum.Cutter
	slice_field_viewer.Slice.SliceOrigin = np.array([0.12247686531188266, 0.2664923667907715, 1.6870545060136746])
	slice_field_viewer.Slice.Normal = np.array([-0.573576436351046, 0.0, 0.8191520442889918])
	slice_field_viewer.UpdateAttributes()
	slice_field_viewer.Data.Mode=5
	slice_field_viewer.Update()
	slice_field_viewer.DecibelScale=0;
	slice_field_viewer.Update()
	slice_field_viewer.ScalarRange=(0,2)
	slice_field_viewer.Update()
	slice_field_viewer.UpdateMax()
	slice_field_viewer.Update()
	document.AllAlgorithms.Add(slice_field_viewer)
	
	#### Surface View #########################################################################################
	
	# Adding a new ModelToGridFilter
	model_to_grid_filter = analysis.core.ModelToGridFilter()
	model_to_grid_filter.Name = sensor.Name
	model_to_grid_filter.Entity = ents['Cerebellum Gray Matter']
	model_to_grid_filter.UpdateAttributes()
	model_to_grid_filter.Update()
	document.AllAlgorithms.Add(model_to_grid_filter)

	# Adding a new FieldInterpolationFilter
	inputs = [user_defined_field_normalizer.Outputs["EM E(x,y,z,f0)"], model_to_grid_filter.Outputs["Surface"]]
	field_interpolation_filter = analysis.core.FieldInterpolationFilter(inputs=inputs)
	field_interpolation_filter.UpdateAttributes()
	field_interpolation_filter.Update()
	
	inputs = [field_interpolation_filter.Outputs["EM E(x,y,z,f0)"]]
	surface_viewer = analysis.viewers.SurfaceViewer(inputs=inputs)
	surface_viewer.Data.Mode=5; surface_viewer.Update()
	surface_viewer.DecibelScale=0; surface_viewer.Update()
	surface_viewer.ScalarRange=(0,2); surface_viewer.Update()
	surface_viewer.UpdateMax(); surface_viewer.Update()
	document.AllAlgorithms.Add(surface_viewer)

def Main():
	
#####################################################################
	# Switches to allow the execution of each block separately
	creates=1 # to create the EM simulation
	voxelize=1 # to voxelize
	run=1      # to execute the simulation     
	analyze=1 # to extract the E-Field
#####################################################################

	# Creates a dictionary of all the entities in the Model
	ents=s4l.model.AllEntities()

	# Creates the EM simulation
	if creates:
		print ('Creating EM Simulation....')
		simulation=CreatesEMSimulation()
		
	# Update the Grid and Creates the Voxels
	if voxelize:
		print ('Creation of Voxels....')
		simulation.UpdateGrid()
		simulation.CreateVoxels()
	
	# Runs the EM simulation	
	if run:
		print ('Execution of the Simulation....')
		simulation.RunSimulation()
		
#	simulation=s4l.document.AllSimulations[1]
		
	# Executes the Post-Processing
	if analyze:
		if simulation.HasResults():
			print ('Initializing PostProcessing....')
			sensor=ents['Flux_Sensor']          # Sensor for flux integration
			entslist=ents['Brain'].Entities     # Group collecting WM, GM and Cerebellum
			Analyze(simulation,sensor,entslist)	
		
if __name__ == '__main__':
	Main()